package com.polaris.live.ui.set

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.BizEventConst
import com.polaris.live.common.event.EventMessage
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityBindGmailBinding
import com.polaris.live.interface_bag.TextWatcherAdapter
import com.polaris.live.translation.R.string
import com.polaris.live.ui.login.BaseGoogleLoginActivity
import com.polaris.live.ui.login.verify.VerifyActivity
import com.polaris.live.viewmodel.login.PhoneNumberModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 绑定邮箱
 *
 * @author Created by lucas on 2024/3/22 07:02
 * @since 1.2.0
 */
class BindGmailActivity : BaseGoogleLoginActivity<PhoneNumberModel, ActivityBindGmailBinding>() {

    private var mGmail = ""

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.bvTitle)
        setStatusBarDarkFont(false)
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        EventBus.getDefault().register(this)
        mViewModel.getIp()

        initListener()
        initObserve()
    }

    private fun initListener() {
        mBinding.etGmail.addTextChangedListener(object : TextWatcherAdapter() {
            override fun afterTextChanged(s: Editable?) {
                val toString = s?.toString()
                val trimmedStr = toString?.replace("\\s+".toRegex(), "")  // 去掉所有空格

                if (trimmedStr.isNullOrEmpty()) {
                    mBinding.loginButton.alpha = AppConst.NOTCLICK
                } else {
                    mBinding.loginButton.alpha = 1.0f
                }
            }
        })

        mBinding.loginButton.setOnSingleClickListener {
            val gmail = mBinding.etGmail.text.toString()
            mGmail = gmail.replace("\\s+".toRegex(), "")  // 去掉所有空格

            if (gmail.isNotEmpty()) {
                mViewModel.isGmailRegistered(mGmail)
            }
        }
        mBinding.gmailClear.setOnSingleClickListener {
            mBinding.etGmail.setText("")
        }
    }

    private fun initObserve() {
        mViewModel.isRegisteredLiveData.observe(this) { state ->
            parseState(state, { resp ->
                if (resp.registered == true) {
                    ToastGravity.showGravity(StringUtils.getString(string.gmail_is_registered))
                    return@parseState
                }
                verifyGoogle {
                    val intent = Intent()
                    intent.setClass(this, VerifyActivity::class.java)
                    intent.putExtra("gmail", mBinding.etGmail.text.toString())
                    intent.putExtra("bind", true)
                    if (it != null) {
                        intent.putExtra("googleValidate", it)
                    }
                    startActivity(intent)
                }
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage) {
        when (event.from) {
            BizEventConst.BIND_SUCCESS -> {//绑定手机号后销毁这页
                finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}